import pymysql

class DBhelper:

    def __init__(self,host,user,password,database,prot=3306,charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = prot
        self.charset = charset

    def update(self,sql,data=None):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        try:
            with db.cursor() as cursor:
                cursor.execute(sql,data)
                db.commit()
                print("修改成功")
        except Exception as e:
            print("数据修改失败",e)
        finally:
            db.close()

    def select(self,sql,data):
        db = pymysql.connect(
            host=self.host,
            user=self.user,
            password=self.password,
            database=self.database,
            port=self.port,
            charset=self.charset
        )
        try:
            with db.cursor() as cursor:
                cursor.execute(sql,data)
                result = cursor.fetchall()
                for i in result:
                    print(i)
        except Exception as e:
            print("查询失败",e)
        finally:
            db.close()


helper = DBhelper('localhost','root','','hrjtpython')
for i in range(10):
    data = (input("请输入姓名:"),int(input("请输入年龄:")),input("请输入性别:"),float(input("请输入身高:")))
    sql = 'insert into person values(%s,%s,%s,%s)'
    helper.update(sql,data)
